<?php

namespace app\common\model\system;


use app\common\model\BaseModel;

/**
 * 上传文件管理
 * Class DemoSystemUploads
 * @package app\demo\model
 */
class SystemUploadsModel extends BaseModel
{
    //
    public string $fieldId = 'id';

    //
    public string $fieldFilename = 'filename';
    // 文件名
    public string $fieldUrl = 'url';
    // 金额
    public string $fieldAssocId = 'assoc_id';
    // 数量
    public string $fieldStatus = 'status';
    // 状态 1:已关联 0未关联
    public string $fieldCreateAt = 'create_at';
    // 创建时间
    protected $name = 'system_uploads';

    // 设置字段信息
    protected $schema = [
        'id' => 'int',        //
        'filename' => 'string',        // 文件名
        'url' => 'string',        // 金额
        'assoc_id' => 'int',        // 数量
        'status' => 'int',        // 状态 1:已关联 0未关联
        'create_at' => 'datetime',        // 创建时间
    ];

    /**
     * 保存上传文件日志
     * @param array $urls
     * @return int|string
     */
    public function saveLog(array $urls)
    {
        $now = date('Y-m-d H:i:s');
        $data = [];
        foreach ($urls as $v) {
            $data[] = ['filename' => basename($v), 'url' => $v, 'create_at' => $now];
        }
        return $this->db()->insertAll($data);
    }

    /**
     * 更新上传文件状态
     * @param array $urls
     * @param int   $id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function updateLog(array $urls, int $id)
    {
        $files = [];
        foreach ($urls as $v) {
            $files[] = basename($urls);
        }
        return $this->db()->whereIn('filename', $files)->update(['status' => 1, 'assoc_id' => $id]);
    }

    /**
     * 删除日志文件
     * @param array $urls
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function deleteLog(array $urls)
    {
        $files = [];
        foreach ($urls as $v) {
            $files[] = basename($v);
        }
        return $this->db()->whereIn('filename', $files)->delete();
    }


}
